package xyz.hzxiao.config.interceptor;

import com.baomidou.mybatisplus.extension.parser.JsqlParserSupport;
import com.baomidou.mybatisplus.extension.plugins.inner.InnerInterceptor;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Map;
import java.util.Objects;

/**
 * 加密拦截器
 *
 * @author ：FuXx
 * @className : xyz.hzxiao.config.interceptor.EncryptInterceptor
 * @since ：2023-08-18
 */
public class EncryptInterceptor extends JsqlParserSupport implements InnerInterceptor {

    @Override
    public void beforePrepare(StatementHandler sh, Connection connection, Integer transactionTimeout) {
        System.out.println("加密拦截器 beforePrepare");
        InnerInterceptor.super.beforePrepare(sh, connection, transactionTimeout);
    }

    @Override
    public void beforeGetBoundSql(StatementHandler sh) {
        System.out.println("加密拦截器 beforeGetBoundSql");
        InnerInterceptor.super.beforeGetBoundSql(sh);
    }

    @Override
    public void beforeQuery(Executor executor, MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) throws SQLException {
        System.out.println("加密拦截器 beforeQuery");
        if (Objects.isNull(parameter) || !(parameter instanceof Map)) {
            return;
        }
        Map paramMap = (Map) parameter;
        for (Object value : paramMap.values()) {
            System.out.println("查询参数：" + value);
        }
    }

    @Override
    public void beforeUpdate(Executor executor, MappedStatement ms, Object parameter) throws SQLException {
        System.out.println("加密拦截器 beforeUpdate");
        InnerInterceptor.super.beforeUpdate(executor, ms, parameter);
    }
}
